package com.weslide.weslideapp.manager.api;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.weslide.weslideapp.Constants;
import com.weslide.weslideapp.WeSlideApplication;
import com.weslide.weslideapp.pojo.ao.LoginAO;
import com.weslide.weslideapp.pojo.ao.ZoneAO;

public class LoginManager {
	private static final String TAG = "LoginManager";
	private WeSlideApplication mApplication;
	private static LoginManager mInstance = null;

	private LoginManager(WeSlideApplication application) {
		this.mApplication = application;
	}

	public synchronized static void init(WeSlideApplication application) {
		if (mInstance == null) {
			mInstance = new LoginManager(application);
		}
	}

	public static LoginManager getInstance() {
		return mInstance;
	}

	public void saveUserInformation(LoginAO ao) {
		SharedPreferences sharePreference = mApplication
				.getSharedPreferences(Constants.SP_KEY_USER_INFORMATION,
						Context.MODE_WORLD_WRITEABLE);
		Editor editor = sharePreference.edit();
		editor.putString(Constants.SP_KEY_USER_INFORMATION_USERID,
				ao.getUserId());
		editor.putString(Constants.SP_KEY_USER_INFORMATION_SCORE, ao.getScore());
		// editor.putString(Constants.SP_KEY_USER_INFORMATION_ADDRESS, ao.ge)
		editor.putString(Constants.SP_KEY_USER_INFORMATION_INVITECODE,
				ao.getInviteCode());
		editor.putString(Constants.SP_KEY_USER_INFORMATION_LEVEL_ID,
				ao.getLevelId());
		editor.putString(Constants.SP_KEY_USER_INFORMATION_LEVEL_NAME,
				ao.getLevelName());
		editor.putString(Constants.SP_KEY_USER_INFORMATION_NAME, ao.getName());
		editor.putString(Constants.SP_KEY_USER_INFORMATION_PHONE, ao.getPhone());
		// editor.putString(Constants.SP_KEY_USER_INFORMATION_REALNAME, value)
		editor.putString(Constants.SP_KEY_USER_INFORMATION_SAVEMONEY,
				ao.getSaveMoney());
		editor.commit();
	}

	public LoginAO getUserInformation() {
		SharedPreferences sharedPreference = mApplication
				.getSharedPreferences(Constants.SP_KEY_USER_INFORMATION,
						Context.MODE_WORLD_WRITEABLE);
		LoginAO ao = new LoginAO();
		ao.setUserId(sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_USERID, ""));
		ao.setAddress(sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_ADDRESS, ""));
		ao.setAddress(sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_INVITECODE, ""));
		ao.setLevelId(sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_LEVEL_ID, ""));
		ao.setLevelName(sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_LEVEL_NAME, ""));
		ao.setName(sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_NAME, ""));
		ao.setPhone(sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_PHONE, ""));
		ao.setSaveMoney(sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_SAVEMONEY, ""));
		ao.setScore(sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_SCORE, ""));
		return ao;
	}

	public void updateScore(String totalScore) {
		SharedPreferences sharedPreference = mApplication
				.getSharedPreferences(Constants.SP_KEY_USER_INFORMATION,
						Context.MODE_WORLD_WRITEABLE);
		sharedPreference.edit()
				.putString(Constants.SP_KEY_USER_INFORMATION_SCORE, totalScore)
				.commit();
	}

	public void updateSaveMoney(String money) {
		SharedPreferences sharedPreference = mApplication
				.getSharedPreferences(Constants.SP_KEY_USER_INFORMATION,
						Context.MODE_WORLD_WRITEABLE);
		sharedPreference.edit()
				.putString(Constants.SP_KEY_USER_INFORMATION_SAVEMONEY, money)
				.commit();
	}

	public void saveAddressInformation(String receiverName,
			String receiverAddress, String receiverPhone) {
		SharedPreferences sharedPreference = mApplication.getSharedPreferences(
				Constants.SP_KEY_RECEIVER_INFORMATION,
				Context.MODE_WORLD_WRITEABLE);
		Editor edit = sharedPreference.edit();
		edit.putString(Constants.SP_KEY_RECEIVER_INFORMATION_NAME, receiverName);
		edit.putString(Constants.SP_KEY_RECEIVER_INFORMATION_ADDRESS,
				receiverAddress);
		edit.putString(Constants.SP_KEY_RECEIVER_INFORMATION_PHONE,
				receiverPhone);
		edit.commit();

	}

	public String getReceiverName() {
		SharedPreferences sharedPreference = mApplication.getSharedPreferences(
				Constants.SP_KEY_RECEIVER_INFORMATION,
				Context.MODE_WORLD_WRITEABLE);
		return sharedPreference.getString(
				Constants.SP_KEY_RECEIVER_INFORMATION_NAME, "");

	}

	public String getReceiverAddress() {
		SharedPreferences sharedPreference = mApplication.getSharedPreferences(
				Constants.SP_KEY_RECEIVER_INFORMATION,
				Context.MODE_WORLD_WRITEABLE);
		return sharedPreference.getString(
				Constants.SP_KEY_RECEIVER_INFORMATION_ADDRESS, "");
	}

	public String getReceiverPhone() {
		SharedPreferences sharedPreference = mApplication.getSharedPreferences(
				Constants.SP_KEY_RECEIVER_INFORMATION,
				Context.MODE_WORLD_WRITEABLE);
		return sharedPreference.getString(
				Constants.SP_KEY_RECEIVER_INFORMATION_PHONE, "");
	}

	public String getTotalScore() {
		SharedPreferences sharedPreference = mApplication
				.getSharedPreferences(Constants.SP_KEY_USER_INFORMATION,
						Context.MODE_WORLD_WRITEABLE);
		return sharedPreference.getString(
				Constants.SP_KEY_USER_INFORMATION_SCORE, "0");
	}

	public boolean isUserLogined() {
		LoginAO ao = getUserInformation();
		if (ao.getUserId() != null && !ao.getUserId().equals("")) {
			return true;
		} else {
			return false;
		}
	}

	public void setUpCurrentCity(ZoneAO ao) {
		SharedPreferences sharedPreference = mApplication
				.getSharedPreferences(Constants.SP_KEY_USER_INFORMATION,
						Context.MODE_WORLD_WRITEABLE);
		Editor edit = sharedPreference.edit();
		Log.i(TAG, "zoneName >>> " + ao.getName());
		edit.putString("zoneName", ao.getName());
		edit.putString("zoneId", ao.getZoneId());
		edit.commit();
	}

	public ZoneAO getCurrentCity() {
		ZoneAO ao = new ZoneAO();
		SharedPreferences sharedPreference = mApplication
				.getSharedPreferences(Constants.SP_KEY_USER_INFORMATION,
						Context.MODE_WORLD_WRITEABLE);
		ao.setName(sharedPreference.getString("zoneName", "全国"));
		ao.setZoneId(sharedPreference.getString("zoneId", "－1"));
		return ao;
	}

	public void removeUserInformation() {
		SharedPreferences sharedPreference = mApplication
				.getSharedPreferences(Constants.SP_KEY_USER_INFORMATION,
						Context.MODE_WORLD_WRITEABLE);
		sharedPreference.edit().clear().commit();
	}
}
